<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/10/2
 * Time: 10:06
 */

namespace app\api\controller;



use think\Db;
header("ACCESS-CONTROL-ALLOW-ORIGIN:http://114.215.68.205");

class PlaySyxw extends Base
{
    public function index(){
        $czid = input("czid",0);
        $ret = array(
            'result'=>0,
            'ResultMsg' => ''
        );
        if( empty($czid) ){
            $ret['ResultMsg'] = '请求参数错误';
            return json_encode($ret,JSON_UNESCAPED_UNICODE);
        }
        $this->qihaoNext($czid);
        $now = time();
        $next = $this->nextResult($czid);
        $datas = [];
        $datas['current']['next_no'] = $next['num'];
        $datas['current']['next_time'] = $next['time'];

        $current = $this->currentHasResult($czid,$now);
        $datas['current']['current_no'] = $current['num'];
        $datas['current']['current_time'] = $current['time'];
        $current_kjjg = str_split($current['kjjg'],2);
        $datas['current']['current_kjjg'] = $current_kjjg;
        $datas['current']['kd'] = intval(max($current_kjjg))-intval(min($current_kjjg));
        $before = $this->currentResult($czid,$now);
        $datas['current']['before_no'] = $before['num'];
        $before_num = Db::name('kjjg_list')->where(['czid'=>$czid,'jztime'=>['<',$current['time']]])->order("jztime DESC")->value("kjjg");
        $datas['current']['ch'] = 0;
        if( !empty($before_num) ){
            $before_kjjg = str_split($before_num,2);
            $datas['current']['ch'] = count(array_intersect($before_kjjg,$current_kjjg));
        }
        $datas['current']['amount'] = Db::name("time")->where(['czid'=>$czid,'jztime'=>['>=',date('H:i:s',time())]])->count();
        $current_sum = array_sum($current_kjjg);
        $datas['current']['sum'] = $current_sum;
        $datas['current']['dx'] = getBigOrSmall($current_sum,30,30);
        $datas['current']['ds'] = getEvenOrOdd($current_sum);

        $list = Db::name('kjjg_list')->where('czid',$czid)->where('kjjg','<>','0')->field("qihao,kjjg,jztime")->order('jztime','DESC')->paginate(11)->each(function($v)use($czid){

            $v['kjjg'] = str_split($v['kjjg'],2);
            $v['sum'] = array_sum($v['kjjg']);
            $v['kd'] = intval(max($v['kjjg']))-intval(min($v['kjjg']));
            $v['dx'] = getBigOrSmall(array_sum($v['kjjg']),30,30);
            $v['ds'] = getEvenOrOdd(array_sum($v['kjjg']));
            $v['ch'] = 0;
            $before_num = Db::name('kjjg_list')->where(['czid'=>$czid,'jztime'=>['<',$v['jztime']]])->order("jztime DESC")->value("kjjg");
            if( !empty($before_num) ){
                $before_kjjg = str_split($before_num,2);
                $v['ch'] = count(array_intersect($before_kjjg,$v['kjjg']));
            }
            unset($v['jztime']);
            return $v;
        })->toArray();
        $list && $list = $list['data'];
        $datas['history'] = $list;
        $ret['result'] = 1;
        $ret['datas'] = $datas;
        return json_encode($ret,JSON_UNESCAPED_UNICODE);
    }

    public function currentPeriod(){
        $czid = input("czid",0);
        $ret = array(
            'result'=>0,
            'ResultMsg' => ''
        );
        if( empty($czid) ){
            $ret['ResultMsg'] = '请求参数错误';
            return json_encode($ret,JSON_UNESCAPED_UNICODE);
        }
        $this->qihaoNext($czid);
        $before_redis = $this->currentResult($czid,time());
        $datas = [
            'kjjg' => [],
            'qihao' => '',
            'jztime' => '',
            'kd' => '',
            'ch' => '',
            'dx' => '',
            'ds' => '',
            'sum' => 0

        ];
        if( !empty($before_redis['kjjg']) ){

            $datas['kjjg'] = str_split($before_redis['kjjg'],2);
            $datas['sum'] = array_sum($datas['kjjg']);
            $datas['qihao'] = $before_redis['num'];
            $datas['jztime'] = $before_redis['time'];
            $datas['kd'] = intval(max($datas['kjjg']))-intval(min($datas['kjjg']));
            $datas['dx'] = getBigOrSmall(array_sum($datas['kjjg']),30,30);
            $datas['ds'] = getEvenOrOdd(array_sum($datas['kjjg']));

            $before_num = Db::name('kjjg_list')->where(['czid'=>$czid,'jztime'=>['<',$before_redis['time']]])->order("jztime DESC")->value("kjjg");
            if( !empty($before_num) ){
                $before_kjjg = str_split($before_num,2);
                $datas['ch'] = count(array_intersect($before_kjjg,$datas['kjjg']));
            }
        }
        $ret['result'] = 1;
        $ret['datas'] = $datas;
        return json_encode($ret,JSON_UNESCAPED_UNICODE);
    }
}